{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "d7100daa",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{1: '我', 2: '你', 3: '他'}"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = [1, 2, 3]\n",
    "b = [\"我\", \"你\", \"他\"]\n",
    "c = dict(zip(a,b))\n",
    "c"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2b3d3e2e",
   "metadata": {},
   "source": [
    "字典的访问"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "2d4691c0",
   "metadata": {},
   "outputs": [
    {
     "ename": "KeyError",
     "evalue": "'你'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mKeyError\u001b[0m                                  Traceback (most recent call last)",
      "\u001b[1;32m~\\AppData\\Local\\Temp/ipykernel_17328/441045256.py\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mc\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m'你'\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mKeyError\u001b[0m: '你'"
     ]
    }
   ],
   "source": [
    "c['你']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "18ca0577",
   "metadata": {},
   "outputs": [
    {
     "ename": "KeyError",
     "evalue": "'2'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mKeyError\u001b[0m                                  Traceback (most recent call last)",
      "\u001b[1;32m~\\AppData\\Local\\Temp/ipykernel_17328/2474434391.py\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mc\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m'2'\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mKeyError\u001b[0m: '2'"
     ]
    }
   ],
   "source": [
    "c['2']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "0c698aba",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'你'"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "c[2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "4d91a5bf",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['我', '爱', '天安门']"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = [1, 2, 3, 4, 5]\n",
    "b = [\"我\", \"你\", \"他\", \"爱\", \"天安门\"]\n",
    "c = dict(zip(b,a))\n",
    "sentence = \"我 爱 天安门\"\n",
    "words = sentence.split(\" \")\n",
    "words"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "108f520d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 4, 5]"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sen_indexs=[]\n",
    "for word in words:\n",
    "    sen_indexs.append(c[word])\n",
    "sen_indexs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "f2fe1eb8",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['我', '爱', '天安门']"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "[word for word in words]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "a3f46d7f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 4, 5]"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "[c[word] for word in words]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "5831ac28",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'我': 1, '你': 2, '他': 3, '爱': 4, '天安门': 5}"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "c"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bfe26af5",
   "metadata": {},
   "source": [
    "添加字典元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "aa55fc63",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'我': 1, '你': 2, '他': 3, '爱': 4, '天安门': 5, '尚学堂': 6}"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "c['尚学堂']=6\n",
    "c"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "39ff14ab",
   "metadata": {},
   "source": [
    "字典的修改"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "7f0b26b4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'我': 1, '你': 2, '他': 3, '爱': 3, '天安门': 5, '尚学堂': 6}"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "c['爱']=3\n",
    "c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "5c314727",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict_items([('我', 1), ('你', 2), ('他', 3), ('爱', 3), ('天安门', 5), ('尚学堂', 6)])"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "c.items()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "c80dc9e0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "('我', 1)\n",
      "('你', 2)\n",
      "('他', 3)\n",
      "('爱', 3)\n",
      "('天安门', 5)\n",
      "('尚学堂', 6)\n"
     ]
    }
   ],
   "source": [
    "for item in c.items():\n",
    "    print(item)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "53eff603",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "我\n",
      "你\n",
      "他\n",
      "爱\n",
      "天安门\n",
      "尚学堂\n"
     ]
    }
   ],
   "source": [
    "for key in c.keys():\n",
    "    print(key)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "fa6b909e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "我 1\n",
      "你 2\n",
      "他 3\n",
      "爱 3\n",
      "天安门 5\n",
      "尚学堂 6\n"
     ]
    }
   ],
   "source": [
    "for key in c.keys():\n",
    "    print(key,c[key])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "8d10307a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "2\n",
      "3\n",
      "3\n",
      "5\n",
      "6\n"
     ]
    }
   ],
   "source": [
    "for value in c.values():\n",
    "    print(value)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "13c298a7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 我 1\n",
      "1 你 2\n",
      "2 他 3\n",
      "3 爱 3\n",
      "4 天安门 5\n",
      "5 尚学堂 6\n"
     ]
    }
   ],
   "source": [
    "for i, key in enumerate(c):\n",
    "    print(i, key, c[key])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "2216062e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 ('我', 1)\n",
      "1 ('你', 2)\n",
      "2 ('他', 3)\n",
      "3 ('爱', 3)\n",
      "4 ('天安门', 5)\n",
      "5 ('尚学堂', 6)\n"
     ]
    }
   ],
   "source": [
    "for i, key in enumerate(c.items()):\n",
    "    print(i,key)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "3abab32f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 我 1\n",
      "1 你 2\n",
      "2 他 3\n",
      "3 爱 3\n",
      "4 天安门 5\n",
      "5 尚学堂 6\n"
     ]
    }
   ],
   "source": [
    "for i, key in enumerate(c.keys()):\n",
    "    print(i,key,c[key])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "239618bb",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "my_test",
   "language": "python",
   "name": "my_test"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
